﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Util;
using BLL;
using Model;

public partial class assEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Util.UtilWeb.CheckRights(this);
            string strId = Request.QueryString["id"];
            LoadView();
            if (!string.IsNullOrWhiteSpace(strId))
                LoadView(int.Parse(strId));
        }
    }

    private void LoadView()
    {
        var lbl = Master.FindControl("lblTitle") as Label;
        lbl.Text = "机构管理";
        var list = new BllAss().GetList(c => true);
        for (int i = 0; i < list.Count; i++)
        {
            ddlAss.Items.Add(new ListItem(list[i].name, list[i].id.ToString()));
        }

    }

    private void LoadView(int id)
    {
        hdId.Value = id.ToString();
        btnAdd.Text = "修改";
        BllAss bll = new BllAss();
        var obj = bll.GetSingle(id);
        this.ddlAss.SelectedValue = obj.parentId.ToString();
        this.ddlSort.SelectedValue = obj.sort.ToString();
        txtName.Text = obj.name;
        txtRemark.Text = obj.remark;
        txtContent.Text = obj.content;
        Blladdr bll1 = new Blladdr();
        var obj1 = bll1.GetSingle(obj.id);
        txtPhone.Text = obj1.phone;
        txtFax.Text = obj1.fax;
        txtPost.Text = obj1.post;
        txtAddr.Text = obj1.addr;
        txtEmail.Text = obj1.email;
        txtCantact.Text = obj1.cantact;
        hdSmall.Value = obj.src;
        imgSmall.Src = "../" + hdSmall.Value;
        imgSmall.Visible = true;
        hdId.Value = obj.id.ToString();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string cmd = btnAdd.Text.Trim();
        BllAss bll = new BllAss();
        Blladdr bll1 = new Blladdr();
        string name = txtName.Text.Trim();
        string remark = txtRemark.Text;
        string content = txtContent.Text;
        string phone = txtPhone.Text;
        string fax = txtFax.Text;
        string post = txtPost.Text;
        string addr = txtAddr.Text;
        string email = txtEmail.Text;
        string cantact = txtCantact.Text;
        string parentId = this.ddlAss.SelectedValue;
        string sort=ddlSort.SelectedValue;
        if (cantact == "" || name == "" || remark == "" || hdSmall.Value == "" || content == "" || phone == "" || fax == "" || post == "" || addr == "" || email == "")
        {
            Util.UtilWeb.Message(this, Util.KoInfo.FillBlank, null);
            return;

        }

        if (cmd == "添加")
        {
            tb_ass obj = new tb_ass();
            tb_addr obj1 = new tb_addr();
            obj.name = name;
            obj.src = hdSmall.Value;
            obj.name = name;
            obj.parentId = int.Parse(parentId);
            obj.remark = remark;
            obj.sort = int.Parse(sort);
            obj.content=content;

            obj1.name = name;
            obj1.phone = phone;
            obj1.post = post;
            obj1.fax = fax;
            obj1.ass = name;
            obj1.email = email;
            obj1.date = DateTime.Now;
            obj1.addr = addr;
            obj1.cantact = cantact;
            if (bll.Add(obj)&&bll1.Add(obj1))
            {
                Response.Redirect("assList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
        if (cmd == "修改")
        {
            int id = int.Parse(hdId.Value);
            tb_ass obj = bll.GetSingle(id);
            tb_addr obj1 = bll1.GetSingle(c => c.ass == obj.name);
            obj.name = name;
            obj.src = hdSmall.Value;
            obj.name = name;
            obj.parentId = int.Parse(parentId);
            obj.remark = remark;
            obj.sort = int.Parse(sort);
            obj.content = content;
            obj1.name = name;
            obj1.phone = phone;
            obj1.post = post;
            obj1.fax = fax;
            obj1.ass = name;
            obj1.cantact = cantact;
            obj1.date = DateTime.Now;
            obj1.addr = addr;
            if (bll.Update(obj)||bll1.Update(obj1))
            {
                Response.Redirect("assList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
    }

    protected void btnUpload1_Click(object sender, EventArgs e)
    {
        string path = Util.UtilWeb.UploadFile(this, fu, null, "../img/pic/");
        hdSmall.Value = path.Replace("../", "");
        imgSmall.Src = "../" + hdSmall.Value;
        imgSmall.Visible = true;
    }
}